package cn.ydw.community.controller;

import cn.ydw.community.entity.Event;
import cn.ydw.community.entity.PageDTO;
import cn.ydw.community.entity.User;
import cn.ydw.community.event.EventProducer;
import cn.ydw.community.service.UserService;
import cn.ydw.community.service.impl.FollowService;
import cn.ydw.community.util.CommonUtils;
import cn.ydw.community.util.HostHolder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import cn.ydw.community.util.CommunityConstant;

import java.util.List;
import java.util.Map;

@Controller
public class FollowController implements CommunityConstant {
    @Autowired
    private FollowService followService;

    @Autowired
    private UserService userService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private EventProducer eventProducer;

    @RequestMapping(path = "/follow", method = RequestMethod.POST)
    @ResponseBody
    public String follow(int entityType, int entityId) {
        User user = hostHolder.getUser();

        followService.follow(user.getId(), entityType, entityId);
        // 点击关注 触发系统通知
        Event event = new Event()
                .setTopic(TOPIC_FOLLOW)
                .setEntityUserId(entityId)
                .setEntityId(entityId)
                .setEntityType(entityType)
                .setUserId(hostHolder.getUser().getId());
        eventProducer.fireEvent(event);

        return CommonUtils.getJSON(0, "已关注!");
    }

    @RequestMapping(path = "/unfollow", method = RequestMethod.POST)
    @ResponseBody
    public String unfollow(int entityType, int entityId) {
        User user = hostHolder.getUser();

        followService.unFollow(user.getId(), entityType, entityId);

        return CommonUtils.getJSON(0, "已取消关注!");
    }

    @GetMapping("/followee/{userId}")
    public String getFolloweePage(@PathVariable("userId")Integer userId, Page page, Model model){
        User user = userService.selectUserById(userId);
        if (user == null){
            throw new RuntimeException("该用户不存在!");
        }
        model.addAttribute("user", user);
        // 设置分页信息
        page.setSize(6);
        page.setTotal(followService.findFolloweeCount(userId, ENTITY_TYPE_USER));
        model.addAttribute("page", page);
        PageDTO pageDTO = new PageDTO(page);
        pageDTO.pageDTO();
        model.addAttribute("pagedto", pageDTO);
        // 获取关注列表
        List<Map<String, Object>> userList = followService.findFollowees(userId, (int) pageDTO.getOffset(), (int) page.getSize());

        if (userList != null) {
            for (Map<String, Object> map : userList) {
                User u = (User) map.get("user");
                map.put("hasFollowed", hasFollowed(u.getId()));
            }
        }
        model.addAttribute("users", userList);
        return "site/followee";
    }

    private boolean hasFollowed(int userId){
        User user = hostHolder.getUser();
        if (user == null){
            return false;
        }
        return followService.hasFollowed(user.getId(), CommunityConstant.ENTITY_TYPE_USER, userId);
    }
    @GetMapping("/follower/{userId}")
    public String getFollowerPage( @PathVariable("userId")Integer userId, Page page, Model model){
        User user = userService.selectUserById(userId);
        if (user == null){
            throw new RuntimeException("该用户不存在!");
        }
        model.addAttribute("user", user);
        // 设置分页信息
        page.setSize(6);
        page.setTotal(followService.findFollowerCount(ENTITY_TYPE_USER, userId));
        model.addAttribute("page", page);
        PageDTO pageDTO = new PageDTO(page);
        pageDTO.pageDTO();
        model.addAttribute("pagedto", pageDTO);
        // 获取关注列表
        List<Map<String, Object>> userList = followService.findFollowers(userId, (int) pageDTO.getOffset(), (int) page.getSize());

        if (userList != null) {
            for (Map<String, Object> map : userList) {
                User u = (User) map.get("user");
                map.put("hasFollowed", hasFollowed(u.getId()));
            }
        }
        model.addAttribute("users", userList);
        return "site/follower";
    }
}
